package token.auth;

import org.json.JSONException;
import org.json.JSONObject;

import token.auth.utils.AESUtils;
import token.auth.utils.HttpUtil;
import token.auth.utils.ThreeDESUtil;

public class AuthService {
	private static String TAG="AuthService";
	//���ܽ��� ��Կ   ���Ż�֮��
	private static final String SEED="d$*13?.ghb#!&bk";
	//���ܷ�ʽ
	private static final String ENCRY_TYPE_AES="1";
	private static final String ENCRY_TYPE_DES="2";
	
	public static final String SPLIT_CHAR=",";
	/**
	 * ����
	 * @param appId
	 * @param appKey
	 * @param encodeType
	 * @return
	 */
	public static String encodeRequest(String appId, String appKey, String encodeType){
		long requestTimeStamp=System.currentTimeMillis();
		System.out.println(TAG+": encode(appId:"+appId+",appKey:"+appKey+",requestTimeStamp:"+requestTimeStamp+",decodeType:"+encodeType+") start");
		String accessKeyStr=appId+SPLIT_CHAR+appKey+SPLIT_CHAR+requestTimeStamp;
		String accessKey=null;
		if(ENCRY_TYPE_AES.equals(encodeType)){
			System.out.println(TAG+": AES encode start");
			accessKey=AESUtils.encrypt(SEED, accessKeyStr);
			System.out.println(TAG+": AES encode end");
		}else if(ENCRY_TYPE_DES.equals(encodeType)){
			System.out.println(TAG+": DES encode start");
			ThreeDESUtil td = new ThreeDESUtil();
			accessKey=td.getEncString(accessKeyStr, SEED);
			System.out.println(TAG+": DES encode end");
		}
		System.out.println(TAG+": encode() end");
		return accessKey;
	}
	
	/**
	 * ��������Ӧ����Ϣ
	 * @param token (appId,cdnToken,responseTimeStamp)
	 * @param decodeType
	 */
	public static String decodeResponse(String token, String decodeType){
		System.out.println(TAG+": decode(token:"+token+", decodeType:"+decodeType+") start");
		String appInfoStr=null;
		if(ENCRY_TYPE_AES.equals(decodeType)){
			System.out.println(TAG+": AES decode start");
			appInfoStr=AESUtils.decrypt(SEED, token);
			System.out.println(TAG+": AES decode end");
		}else if(ENCRY_TYPE_DES.equals(decodeType)){
			System.out.println(TAG+": DES decode start");
			ThreeDESUtil td = new ThreeDESUtil();
			appInfoStr=td.getDecString(token, SEED);
			System.out.println(TAG+": DES decode end");
		}
		System.out.println(TAG+": decode() end");
		return appInfoStr;
	}
	
	public static void main(String[] args) {
//		JSONObject returnJson=HttpUtil.executeHttpGet("http://dev.kuaidaili.com/api/getproxy/?orderid=956785625794622&num=100&b_pcchrome=1&b_pcie=1&b_pcff=1&protocol=1&method=2&an_an=1&an_ha=1&sp1=1&quality=1&sort=1&dedup=1&format=json&sep=1");
//		System.out.println(returnJson);
		
		String appId="abefe3a4-27e4-11e6-9089-047d7becc";
		String appKey="c3477aff-27e4-11e6-9089-047d7becc1fa";
		String encodeType=ENCRY_TYPE_AES;
		String accessKey=AuthService.encodeRequest(appId, appKey, encodeType);
		JSONObject returnJson=HttpUtil.executeHttpPost("http://localhost:8080/auth_service/auth?accessKey="+accessKey+"&decodeType="+encodeType, null, null, null, null);
		try {
			String token=(String)returnJson.getString("token");
			String appInfo=decodeResponse(token,encodeType);
			System.out.println(appInfo);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
